'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 英雄天赋
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var rewardService = require('../../../services/rewardService');
var propertyConf = require('../../../config/propertyConf');
var roleService = require('../../../services/roleService');
var vipService = require('../../../services/vipService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 切换英雄天赋
 */
Handler.prototype.setTalent = function (msg, session, next) {
    var role_id = session.uid;
    var talent_id = msg.talent_id;

    if (!talent_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var hero_talent = 0;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'vip', 'build', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;

                // 切换英雄天赋
                if (!role_model.setHeroTalent(talent_id)) {
                    return next(null, {
                        code: Code.USER_NOT_TALENT_OR_NOT_SAVE
                    });
                }
                // 加入切换天赋实际值

                hero_talent = role_model.getHeroTalent();
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    hero_talent: hero_talent
                }
            });
        }
    );
};
